package com.fingard.dsp.bank.directbank.abc02.receiptpro;

import java.io.File;
import java.util.Date;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directConfig.ReceiptProConfig;
import com.fingard.dsp.bank.directbank.abc02.ABC02Header;
import com.fingard.dsp.bank.directbank.abc02.ABC02ReceiptPro;
import com.fingard.dsp.bank.reqrespobj.downloadreceipt.ReqQryReceipt;
import com.fingard.dsp.bank.reqrespobj.downloadreceipt.RetAtsReceipt;
import com.fingard.dsp.bank.sepro.ReceiptPro;
import com.fingard.io.FileHelper;
import com.fingard.text.StringHelper;
import com.fingard.util.SmbFileUtil;
import com.fingard.util.ZipHelper;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**先发送CMRA25报文，再取回单压缩文件*/
public class ABC02ReceiptProCMRA25 extends ABC02ReceiptPro{
	public RetAtsReceipt qryReceiptOneDay(ReceiptProConfig pConfig, ReqQryReceipt pReqReceipt, Date pOneDay) throws Exception {
	
		String qryDay8 = Format.Date8Format.format(pOneDay);
		// 赋值通用参数
        ABC02Header bankHeader = new ABC02Header();
        bankHeader.CCTransCode = "CMRA25";
        bankHeader.ReqSeqNo = reqHeader.transSeqID;
        XmlTextWriter xmlWriter = new XmlTextWriter();// 发银行报文的XML对象
        writeCommonHeader(xmlWriter, bankHeader);
        xmlWriter.writeStartDocument();
        xmlWriter.writeStartElement("Corp");
        xmlWriter.writeElementString("StartDate", qryDay8);
        xmlWriter.writeElementString("AlterFlag", "1");
        xmlWriter.writeEndElement();
        xmlWriter.writeEndDocument();
        // 调用接口、zip包自动上传到前置机文件夹里面
        String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
        if (tmpStrRet[0].length() == 0) {
            StringBuilder tmpSbMsg = new StringBuilder();
            XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
            if (tmpBkReader != null) {
                String fileFlag = tmpBkReader.getFirstText("/ap/FileFlag");
                // 判断是否有文件包
                if (fileFlag != null && !fileFlag.equals("0")) {
                    String fileName = tmpBkReader.getFirstText("/ap/Cmp/BatchFileName");
                    //临时下载解压目录
            		String saveDirPathTemp = pConfig.getSaveSubDir(pReqReceipt.reqHeader, pOneDay) + File.separator + qryDay8 + File.separator + ReceiptPro.getTransID();
                    FileHelper.mkdirsIfNotExists(saveDirPathTemp);
                    String destFilePath;
                    String needDelFilePath = "";
            		if(bankFront.bankReceiptMainDir.toLowerCase().contains("smb://")){
            			destFilePath = saveDirPathTemp + File.separator + fileName;
	            		WriteBankLogLn("从前置机目录:" + bankFront.bankReceiptMainDir + "下载回单文件到:" + destFilePath);
	            		StringBuilder smbSb = new StringBuilder();
	                    if (1 != SmbFileUtil.smbDownload(saveDirPathTemp, bankFront.bankReceiptMainDir, fileName, smbSb)) {
	                    	WriteBankLogLn("下载失败："+smbSb.toString());
	                    	return null;
	                    }
	                    needDelFilePath = destFilePath;
            		}else{
            			//WriteBankLogLn("从前置机目录:" + bankFront.bankReceiptMainDir + "复制回单文件到:" + saveDirPath);
            			//String zipFilePath = StringHelper.trimEndFileSp(bankFront.bankReceiptMainDir)+File.separator+fileName;
            			//File srcFile = new File(zipFilePath);
            			//FileHelper.copyFileWithCompare(srcFile, destFilePath);
            			destFilePath = StringHelper.trimEndFileSp(bankFront.bankReceiptMainDir)+File.separator+fileName;
            			if(!FileHelper.existFile(destFilePath)){
            				WriteBankLogLn("前置目录下不存在该文件："+destFilePath);
	                    	return null;
            			}
            		}
            		
                    WriteBankLogLn("开始解压文件" + fileName);
                    ZipHelper.unzipFile(destFilePath, saveDirPathTemp);
                    getPdfFiles(pConfig, pReqReceipt, pOneDay, saveDirPathTemp);
                    if(needDelFilePath.length()>0 && pConfig.deleteAfterUploaded){
                    	WriteBankLogLn("删除"+destFilePath);
                    	FileHelper.deleteFileIfExists(destFilePath);
                    }
                    WriteBankLogLn("删除"+saveDirPathTemp);
                    FileHelper.deleteRecursive(saveDirPathTemp);
                } else {
                	WriteBankLogLn(tmpBkReader.getFirstText("/ap/RespInfo"));
                    WriteBankLogLn(tmpBkReader.getFirstText("/ap/RxtInfo"));
                }
            } else {
                WriteBankLogLn(tmpSbMsg.toString());
            }
        } else {
            WriteBankLogLn("未正常获取银行数据");
        }
		
		return null;
	}

}
